package com.sakura.hk.sakura.springboot.plugin.task;

import com.sakura.hk.sakura.springboot.plugin.SakuraExtension;
import com.sakura.hk.sakura.springboot.plugin.config.SakuraPluginConfig;
import com.sakura.hk.sakura.springboot.plugin.model.ClassJavaFileManager;
import com.sakura.hk.sakura.springboot.plugin.model.ClassJavaFileObject;
import com.sakura.hk.sakura.springboot.plugin.model.StringObject;
import com.sakura.hk.sakura.springboot.plugin.tool.GenerateClassBytes;
import com.sakura.hk.sakura.springboot.plugin.tool.GenerateJavaString;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.internal.AbstractTask;
import org.gradle.api.tasks.TaskAction;

import javax.tools.*;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 生成我的启动主类编译文件
 *
 * @Author: 贺坤
 * @Date: 2021/7/13 13:53
 */
public class GenerateMyMainClassFile extends DefaultTask {
    /**
     * 插件启动类 类名
     */
    private String myLauncherName;
    /**
     * 包名
     */
    private String myLauncherPackageName;
    /**
     * 插件路径属性
     */
    private String pluginPathProperty;
    /**
     * 插件文件夹属性
     */
    private String pluginPathNameProperty;
    /**
     * 插件所在文件夹名称
     */
    private String pluginPathName;
    /**
     * Springboot 启动类的jar包路径
     */
    private String springBootLoaderJarPath;
    /**
     * 换行
     */
    private static final String LINE_FEED = "\r\n";
    /**
     * 缓存目录
     */
    private File cacheDir;
    /**
     * 缓存目录名称
     */
    private String cacheName;
    /**
     * 是否开启自定义
     */
    private Boolean custom;
    /**
     * 当前任务名称
     */
    public static final String MY_TASK_NAME = "generateMyMainClassFile";

    /**
     * 当前任务的具体执行
     *
     * @throws URISyntaxException URI 地址异常
     */
    @TaskAction
    public void action() throws URISyntaxException {
        // 初始化
        init();
        // 检查必备项
        checkRequiredItems();
        // 得到java 原文件
        String mainJavaString = GenerateJavaString.generateMainJavaString(LINE_FEED, myLauncherPackageName, myLauncherName, pluginPathProperty, pluginPathName, pluginPathNameProperty);
        // 得到编译文件
        byte[] mainClassBytes = GenerateClassBytes.getMainClassBytes(mainJavaString, myLauncherName, springBootLoaderJarPath, custom);
        // 检查缓存目录是否存在
        File cacheFile = new File(cacheDir, cacheName);
        // 检查包路径是否存在
        String[] split = myLauncherPackageName.split("\\.");
        for (String child : split) {
            cacheFile = new File(cacheFile, child);
        }
        if (!cacheFile.exists()) {
            cacheFile.mkdirs();
        }
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(new File(cacheFile, myLauncherName + ".class"));
            fileOutputStream.write(mainClassBytes);
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 调用以注入插件
     *
     * @param project 项目对象
     * @return  注入的任务
     */
    public static AbstractTask foundMyTask(Project project) {
        GenerateMyMainClassFile task = project.getTasks().create(MY_TASK_NAME, GenerateMyMainClassFile.class);
        task.dependsOn(DecompressionSpringbootBuild.MY_TASK_NAME);
        task.setDescription("生成启动类");
        return task;
    }

    /**
     * 初始化相关变量
     */
    private void init() {
        SakuraExtension sakuraExtension = getProject().getExtensions().getByType(SakuraExtension.class);
        this.myLauncherName = sakuraExtension.getMyLauncherName();
        this.myLauncherPackageName = sakuraExtension.getMyLauncherPackageName();
        this.pluginPathProperty = sakuraExtension.getPluginPathProperty();
        this.pluginPathName = sakuraExtension.getPluginPathName();
        this.springBootLoaderJarPath = sakuraExtension.getSpringBootLoaderJarPath();
        this.cacheDir = sakuraExtension.getCacheDir();
        this.cacheName = sakuraExtension.getCacheName();
        this.custom = sakuraExtension.getCustom();
        this.pluginPathNameProperty = sakuraExtension.getPluginPathNameProperty();
    }

    /**
     * 检查插件必填项
     */
    private void checkRequiredItems() {
        if (custom) {
            if (springBootLoaderJarPath == null) {
                throw new RuntimeException("springBootLoaderJarPath 不能为 null");
            }
            File file = new File(springBootLoaderJarPath);
            if (!file.exists()) {
                throw new RuntimeException("springBootLoaderJarPath 所描述的文件不存在");
            }
        }
    }


}
